
package com.bk.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.bk.dao.impl.UserDaoImpl;
import com.bk.model.User;
import com.bk.service.UserService;
import com.bk.utils.MD5Utils;
import com.sun.xml.internal.bind.annotation.OverrideAnnotationOf;

public class UserServiceImpl implements UserService{

	private UserDaoImpl userDaoImpl=new UserDaoImpl();
	
	@Override
	public boolean findByName(String uname) throws SQLException {
		
		User u=userDaoImpl.findByName(uname);
		if(u==null){
			return false;
		}
		//TODO
		return true;
			
	}
	
	@Override
	public boolean register(User u) throws SQLException {
		String sql="insert into user values(?,?,?,?,?,?,?,?,?,?,?);";
		Object[] params=new Object[]{u.getUid(),u.getUname(),u.getUpwd(),u.getRealname(),u.getUemail(),
				u.getUtel(),u.getUbirthday(),u.getUgender(),u.getState(),u.getCode(),u.getUintroduce()};
		int count=userDaoImpl.add(sql, params);
		if(count==1){
			return true;
		}
		//TODO
		return false;
			
	}
	@Override
	public boolean active(String uid, String code) throws SQLException {
		List<User> list=userDaoImpl.findByOthers("select * from user where uid=? and code=?", uid,code);
		//TODO
		if(list.size()>0){
			return true;
		}
		return false;
			
	}
	@Override
	public boolean active1(String uid) throws SQLException {
		int count = userDaoImpl.update("update user set state=?,code=? where uid=?","1","null",uid);
		//TODO
		if(count==1){
			return true;
		}
		return false;
			
	}

	@Override
	public User login(String uname, String upwd) throws SQLException{
		
		List<User> list = userDaoImpl.findByOthers("select * from user where uname=? and upwd =? and state=?", uname,MD5Utils.md5(upwd),"1");
		if(list.size()>0){
			return list.get(0);
		}
		return null;
			
	}
}

	